using EFCoreDemo.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace EFCoreDemo.Services;

public class DemoContext : DbContext {
    public DbSet<DemoModel> DemoModels { get; set; }

    public DemoContext(DbContextOptions<DemoContext> options) :
        base(options) { }

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        modelBuilder.ApplyConfiguration(new DemoModelConfiguration());
    }
}

public class DemoModelConfiguration : IEntityTypeConfiguration<DemoModel> {
    public void Configure(EntityTypeBuilder<DemoModel> builder) {
        builder.ToTable("demomodels");

        builder.HasKey(p => p.Id);

        builder.Property(p => p.Name).IsRequired();
    }
}

public class
    DemoContextDesignFactory : IDesignTimeDbContextFactory<DemoContext> {
    public DemoContext CreateDbContext(string[] args) =>
        new(new DbContextOptionsBuilder<DemoContext>()
            .UseSqlServer(
                "Server=.;Initial Catalog=DemoDb;Integrated Security=true")
            .Options);
}